﻿using System;
using System.Windows.Input;
using System.Windows.Media;

namespace HeritageAcademy.Library
{
    /// <summary>
    /// Natural number selector class.
    /// </summary>
    public partial class NaturalNumberSelector
    {
        #region Parameters

        private double mousePositionY = -1;
        private readonly double sensetivity;
        private int maxValue;
        private int minValue;
        private int currentValue;
        private string currentValueDecorator = String.Empty;

        #endregion

        #region Ctor

        public NaturalNumberSelector()
        {
            InitializeComponent();
            sensetivity = ActualHeight/3;
            MouseUp += NaturalNumberSelector_MouseUp;
            MouseDown += NaturalNumberSelector_MouseDown;
            MouseMove += NaturalNumberSelector_MouseMove;
        }

        #endregion

        #region Properties

        public int MaxValue
        {
            get { return maxValue; }
            set { maxValue = value; }
        }

        public int MinValue
        {
            get { return minValue; }
            set { minValue = value; }
        }

        public int CurrentValue
        {
            get { return currentValue; }
            set
            {
                currentValue = value;
                UpdateNumber();
            }
        }

        public string CurrentValueDecorator
        {
            get { return currentValueDecorator; }
            set
            {
                currentValueDecorator = value;
                UpdateNumber();
            }
        }

        public double CurrentValueFontSize
        {
            get { return tbNumber.FontSize; }
            set { tbNumber.FontSize = value; }
        }

        public SolidColorBrush CurrentValueColor
        {
            get { return (SolidColorBrush) tbNumber.Foreground; }
            set { tbNumber.Foreground = value; }
        }

        #endregion

        #region Event Handlers

        public void NaturalNumberSelector_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousePositionY <= 0) return;
            var currentY = e.GetPosition(this).Y;
            var deltaY = currentY - mousePositionY;
            if (deltaY < 0 && Math.Abs(deltaY) > sensetivity)
            {
                if (currentValue == maxValue) currentValue = minValue - 1; // loop      
                ++currentValue;
                tbNumber.Text = currentValue + currentValueDecorator;
                mousePositionY = -1;
                RaiseSelectorChangedValueUp();
            }
            if (deltaY > 0 && Math.Abs(deltaY) > sensetivity)
            {
                if (currentValue == minValue) currentValue = maxValue + 1; // loop
                --currentValue;
                tbNumber.Text = currentValue + currentValueDecorator;
                mousePositionY = -1;
                RaiseSelectorChangedValueDown();
            }
        }

        private void NaturalNumberSelector_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mousePositionY = e.GetPosition(this).Y;
        }

        private void NaturalNumberSelector_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePositionY = -1;
        }

        public static event EventHandler SelectorChangedValueUp;

        private static void RaiseSelectorChangedValueUp()
        {
            if (SelectorChangedValueUp != null)
            {
                SelectorChangedValueUp(null, new EventArgs());
            }
        }

        public static event EventHandler SelectorChangedValueDown;

        private static void RaiseSelectorChangedValueDown()
        {
            if (SelectorChangedValueDown != null)
            {
                SelectorChangedValueDown(null, new EventArgs());
            }
        }

        #endregion

        #region Private Methods

        private void UpdateNumber()
        {
            tbNumber.Text = currentValue + currentValueDecorator;
        }

        #endregion
    }
}